package com.asset.servlet.stock;
/**
 *@ClassName FindAllServlet
 *@Author B21015118吴逸鑫
 *@Date 2022/6/9 20:05
 **/
import com.asset.bean.PageData;
import com.asset.service.StockService;
import com.asset.service.impl.StockServiceImpl;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**查找全部**/
@WebServlet("/findAllServlet2")
public class FindAllServlet2 extends HttpServlet {
    StockService bookService = new StockServiceImpl();
    /****/
    /**分页查找**/
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//                PrintWriter out = response.getWriter();
        String pageNum = request.getParameter("pageNum");
        String pageSize = request.getParameter("pageSize");
        if(null == pageNum || "".equals(pageNum)) {
            pageNum = "1";
        }
        if(null == pageSize || "".equals(pageSize)) {
            pageSize = "4";
        }
        PageData findAll = bookService.findAll(Integer.parseInt(pageNum), Integer.parseInt(pageSize));

        if (Integer.parseInt(pageNum) == 1) {
            findAll.setHasPrePage(false);
        } else {
            findAll.setHasPrePage(true);
        }
        if (Integer.parseInt(pageNum) == findAll.getTotal()) {
            findAll.setHasNextPage(false);
        } else {
            findAll.setHasNextPage(true);
        }
        request.setAttribute("page", findAll);
        request.getRequestDispatcher("/user_list2.jsp").forward(request, response);
//                JSONObject json = new JSONObject();
//                json.put("code",0);
//                json.put("msg","success");
//                json.put("count",findAll.getTotal());
//                json.put("data",findAll);
//                out.print(json.toJSONString());
//                out.flush();
//                out.close();
    }

    /**无分页查找**/
//        @Override
//        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//                bookService.findAll(request, response);
//
//        }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        doGet(request, response);
    }

}

